# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import coin.exchange.base.kr_rest.native_public_client_base as npubcb
import coin.exchange.okex.kr_rest.constants as exchange_constants


class OkexNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self, timeout=3):
    npubcb.NativePublicClientBase.__init__(self, url="https://www.okex.com/api/v1", timeout=timeout)
    self._valid_kline_period = ['1min', '3min', '5min', '15min', '30min', \
      '1day', '1week', '1hour', '2hour', '4hour', '6hour', '12hour']

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
        'Invalid kline period!'

  def get_ticker(self, symbol):
    return self._get_as_json("ticker.do", params=dict(symbol=symbol))

  def get_orderbook(self, symbol, size=5):
    assert 1 <= size <= 200, 'Invalid size.'
    params = dict(symbol=symbol, size=size)
    return self._get_as_json("depth.do", params)

  def get_history_kline(self, symbol, kline_period, size=1, since=None):
    self.check_kline_period(kline_period)
    url = "kline.do"
    params = {
        'symbol': symbol,
        'type': kline_period,
    }
    if size:
      params['size'] = size
    if since:
      params['since'] = since
    return self._get_as_json(url, params)

  def queryAll(self):
    for symbol in exchange_constants.symbol_list[:10]:
      print(self.get_orderbook(symbol))


if __name__ == "__main__":
  client = OkexNativePublicClient()
  print(client.get_ticker('ltc_btc'))
  print(client.get_history_kline('btc_usdt', '1hour', 5))
  client.queryAll()
